package com.happy3w.ssp.schoolmatehome.repository;

import com.happy3w.ssp.schoolmatehome.service.note.NoteStateSummary;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@RequiredArgsConstructor
@Repository
public class NoteStateRepository {
    private final JdbcTemplate jdbcTemplate;


    public NoteStateSummary findById(Long id, Long curUserId) {
        return jdbcTemplate.queryForObject(
                """
                        SELECT view_count, favorite_count,collect_count,
                        nf.id is not null as isFavorite,
                        nc.id is not null as isCollect,
                        nt.id is not null as isTemplate
                        FROM note_state ns
                        left join note_favorite nf on nf.note_id = ns.note_id and nf.user_id=?
                        left join note_collect nc on nc.note_id = ns.note_id and nc.user_id=?
                        left join note_template nt on nt.note_id = ns.note_id and nt.user_id=?
                        WHERE ns.note_id = ?""",
                BeanPropertyRowMapper.newInstance(NoteStateSummary.class),
                curUserId,curUserId,curUserId,
                id);
    }

    public void initState(Long id) {
        jdbcTemplate.update("INSERT INTO note_state (note_id, view_count, favorite_count, collect_count) VALUES (?, ?, ?, ?)",
                id, 0, 0, 0);
    }

    public void markRead(Long noteId, Long fanId) {
        jdbcTemplate.update("""
                        INSERT INTO note_view (note_id, user_id, updated_at)
                        VALUES (?, ?, NOW())
                        ON DUPLICATE KEY UPDATE updated_at = VALUES(updated_at);
                        """,
                noteId, fanId);
        jdbcTemplate.update("UPDATE note_state SET view_count = view_count + 1 WHERE note_id = ?", noteId);
    }

    @Transactional
    public void markFavorite(Long noteId, Long fanId) {
        int count = jdbcTemplate.update("INSERT IGNORE INTO note_favorite (note_id, user_id, created_at) VALUES (?, ?, NOW())",
                noteId, fanId);
        if (count > 0) {
            jdbcTemplate.update("UPDATE note_state SET favorite_count = favorite_count + 1 WHERE note_id = ?", noteId);
        }
    }

    @Transactional
    public void unmarkFavorite(Long noteId, Long fanId) {
        int count = jdbcTemplate.update("delete from note_favorite where note_id=? and user_id=?",
                noteId, fanId);
        if (count > 0) {
            jdbcTemplate.update("UPDATE note_state SET favorite_count = favorite_count - 1 WHERE note_id = ?", noteId);
        }
    }

    @Transactional
    public void markCollect(Long noteId, Long fanId) {
        int count = jdbcTemplate.update("INSERT IGNORE INTO note_collect (note_id, user_id, created_at) VALUES (?, ?, NOW())",
                noteId, fanId);
        if (count > 0) {
            jdbcTemplate.update("UPDATE note_state SET collect_count = collect_count + 1 WHERE note_id = ?", noteId);
        }
    }

    @Transactional
    public void unmarkCollect(Long noteId, Long fanId) {
        int count = jdbcTemplate.update("delete from note_collect where note_id=? and user_id=?",
                noteId, fanId);
        if (count > 0) {
            jdbcTemplate.update("UPDATE note_state SET collect_count = collect_count - 1 WHERE note_id = ?", noteId);
        }
    }

    public void markTemplate(Long userId, Long noteId) {
        jdbcTemplate.update("INSERT IGNORE INTO note_template (user_id, note_id, created_at) VALUES (?, ?, NOW())",
                userId, noteId);
    }

    public void unmarkTemplate(Long userId, Long noteId) {
        jdbcTemplate.update("delete from note_template where user_id=? and note_id=?",
                userId, noteId);
    }

    public void deleteByNoteId(Long id) {
        jdbcTemplate.update("delete from note_state where note_id=?", id);
        jdbcTemplate.update("delete from note_view where note_id=?", id);
        jdbcTemplate.update("delete from note_favorite where note_id=?", id);
        jdbcTemplate.update("delete from note_collect where note_id=?", id);
        jdbcTemplate.update("delete from note_template where note_id=?", id);
    }
}
